<?php
include_once 'function.php';
include 'register.inc.php';

if (isset($_POST['resend']) && !empty($_POST['resend']))
{
	$email = $_POST['resend'];
	$conn = conn();
	$esc_email = mysql_real_escape_string($email, $conn);
	$sql = "SELECT active_key, name FROM accounts WHERE email = '$esc_email'";
	$result = mysql_query($sql, $conn);
	$row = mysql_fetch_assoc($result);
	if (!empty($row['active_key']))
	{
		$rand = $row['active_key'];
	}
	else
	{
		// create random string and update accounts table
		$shuffled = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz');
		$rand = substr($shuffled, 0, 32);
		$sql = "UPDATE accounts SET active_key = '$rand' WHERE email = '$esc_email'";
		mysql_query($sql, $conn);
	}
	$name = empty($row['name']) ? substr($email, 0, strpos($email, '@')) : $row['name'];
	send_active_email($email, $name, $rand);
	
	exit;
}

if (!isset($_GET['email']) || empty($_GET['email']))
	exit;

$email = $_GET['email'];
?><!DOCTYPE HTML>
<html>
	<head>
		<link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<meta name="keywords" content="" />
		<meta name="description" content="" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="theme.css" />
		<style type="text/css">
			ol li {
				margin-top: 10px;
			}
		</style>
		<script src="/jquery.min.js"></script>
		<script language="javascript">
			$(document).ready(function() {
				$('#resend').click(function() {
					$.ajax({
						url: 'active.php', 
						type: 'POST',
						data: {resend: '<?php echo $email; ?>'},
						dataType: 'text',
						success: function() {
							$('#resend').after('<span style="margin-left:10px;color:red;">发送成功</span>');
							$('#resend').next().fadeOut(3000, function() {$('#resend').next().remove();});
						}
					});
				});
			});
		</script>
	</head>
	<body>
		<div class="wrapper">
			<div style="margin-left:180px;">
				<h1 style="margin:0px;margin-bottom:20px;">查收确认信</h1>
确认信已经发到你的邮箱 <span style="color:#620091"><?php echo $email;?></span> ，你需要点击邮件中的确认链接来完成注册。
				<h2 style="color:#620091;margin:0px;margin-top:80px;font-size:14px;font-weight:normal;">没有收到确认信怎么办？······</h2>
				<ol>
					<li>检查一下上面Email地址是否正确，错了就<a href="/register.php">重新注册</a>一次吧:)</li>
					<li>看看是否在邮箱的垃圾箱里</li>
					<li>稍等几分钟，若仍旧没收到确认信，让<?php echo SITE;?><a id="resend" href="###">重发一封</a></li>
				</ol>
			</div>
		</div>
	</body>
</html>